package com.bootdo.kaoqin.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.haojiangboutils.MapBuild;
import com.bootdo.kaoqin.domain.KaoqinCallGroup;
import com.bootdo.kaoqin.service.KaoqinCallGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.kaoqin.domain.KaoqinConfig;
import com.bootdo.kaoqin.service.KaoqinConfigService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import sun.security.provider.SHA;

/**
 * 
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-09-27 20:53:35
 */
 
@Controller
@RequestMapping("/kaoqin/kaoqinConfig")
public class KaoqinConfigController extends BaseController{


	@Autowired
	private KaoqinConfigService kaoqinConfigService;

	@Autowired
	private KaoqinCallGroupService kaoqinCallGroupService;

	
	@ModelAttribute
	public KaoqinConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return kaoqinConfigService.get(id);
		}else{
			return new KaoqinConfig();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/kaoqin/kaoqinConfig','kaoqin:kaoqinConfig:kaoqinConfig',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','kaoqin:kaoqinConfig:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("kaoqin:kaoqinConfig:kaoqinConfig")	
	String KaoqinConfig(KaoqinConfig kaoqinConfig,HttpServletRequest request,HttpServletResponse response){
	    return "kaoqin/kaoqinConfig/kaoqinConfig";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("kaoqin:kaoqinConfig:kaoqinConfig")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<KaoqinConfig> kaoqinConfigList = kaoqinConfigService.list(query);
		int total = kaoqinConfigService.count(query);
		PageUtils pageUtils = new PageUtils(kaoqinConfigList, total);
		return pageUtils;
	}

	/**
	 * /kaoqin/kaoqinConfig/getGroupList
	 * @param params
	 * @return
	 */
	@ResponseBody
	@PostMapping("/getGroupList")
	@RequiresPermissions("kaoqin:kaoqinConfig:kaoqinConfig")
	public R getGroupList(@RequestParam Map<String, Object> params){
		List<KaoqinCallGroup> kaoqinCallGroups =  kaoqinCallGroupService.list(MapBuild.getInstand().setKV("userId",ShiroUtils.getUserId()));
		return R.ok().put("l",kaoqinCallGroups);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("kaoqin:kaoqinConfig:edit")
	String add(KaoqinConfig kaoqinConfig,HttpServletRequest request,HttpServletResponse response){
	    return "kaoqin/kaoqinConfig/kaoqinConfigedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(KaoqinConfig kaoqinConfig) {
		return R.ok().put("i",kaoqinConfig);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("kaoqin:kaoqinConfig:edit")
	public R save( KaoqinConfig kaoqinConfig){
		if(null == kaoqinConfig.getUserId()){
			kaoqinConfig.setUserId(ShiroUtils.getUserId());
			kaoqinConfig.setUserName(ShiroUtils.getUser().getUsername());
		}
		if(null != kaoqinConfig.getGroupId()){
			KaoqinCallGroup	kaoqinCallGroup =  kaoqinCallGroupService.get(kaoqinConfig.getGroupId());
			if(null != kaoqinCallGroup){
				kaoqinConfig.setGroupName(kaoqinCallGroup.getName());
			}
		}
		if(kaoqinConfigService.save(kaoqinConfig)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("kaoqin:kaoqinConfig:edit")
	public R remove( Long id){
		if(kaoqinConfigService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("kaoqin:kaoqinConfig:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		kaoqinConfigService.batchRemove(ids);
		return R.ok();
	}
	
}